﻿using System;
using System.Collections.Generic;
using Microsoft.BizTalk.Operations;
using System.Management;
using System.Diagnostics;

namespace BizTalkOneService
{
    class BizTalkInstances
    {
        EventLog appLog = new EventLog();
        BizTalkOperations bto = new BizTalkOperations();
        public int All { get; set; }
        public int Active { get; set; }
        public int Completed { get; set; }
        public int Dehydrated { get; set; }
        public int ReadyToRun { get; set; }
        public int RunningAll { get; set; }
        public int Scheduled { get; set; }
        public int Suspended { get; set; }
        public int SuspendedAll { get; set; }
        public int SuspendedNotResumable { get; set; }
        public int Terminated { get; set; }


        public BizTalkInstances()
        {
            try
            {
                var instances = bto.GetServiceInstances();
                foreach (Instance instance in instances)
                {
                    if (instance.InstanceStatus == InstanceStatus.All)
                        All++;
                    if (instance.InstanceStatus == InstanceStatus.Active)
                        Active++;
                    if (instance.InstanceStatus == InstanceStatus.Completed)
                        Completed++;
                    if (instance.InstanceStatus == InstanceStatus.Dehydrated)
                        Dehydrated++;
                    if (instance.InstanceStatus == InstanceStatus.ReadyToRun)
                        ReadyToRun++;
                    if (instance.InstanceStatus == InstanceStatus.RunningAll)
                        RunningAll++;
                    if (instance.InstanceStatus == InstanceStatus.Scheduled)
                        Scheduled++;
                    if (instance.InstanceStatus == InstanceStatus.Suspended)
                        Suspended++;
                    if (instance.InstanceStatus == InstanceStatus.SuspendedAll)
                        SuspendedAll++;
                    if (instance.InstanceStatus == InstanceStatus.SuspendedNotResumable)
                        SuspendedNotResumable++;
                    if (instance.InstanceStatus == InstanceStatus.Terminated)
                        Terminated++;
                   
                }
            }
            catch (Exception ex)
            {
                appLog.Source = Settings.EventLogSource;
                EventLogEntryType eventType = EventLogEntryType.Error;
                appLog.WriteEntry("Error retrieving instance status: " + ex.Message.ToString() + "\n" + ex.StackTrace, eventType);  
            
            }
        }
        
        public List<Counter> GetInstances()
        {
            List<Counter> counterList = new List<Counter>();
            counterList.Add(new Counter { CounterCategory = "Instance" ,CounterName = "Active", CounterValue = Active });
            counterList.Add(new Counter { CounterCategory = "Instance" ,CounterName = "Dehydrated", CounterValue = Dehydrated });
            counterList.Add(new Counter { CounterCategory = "Instance" ,CounterName = "ReadyToRun", CounterValue = ReadyToRun });
            counterList.Add(new Counter { CounterCategory = "Instance", CounterName = "Completed", CounterValue = Completed });
            counterList.Add(new Counter { CounterCategory = "Instance", CounterName = "Terminated", CounterValue = Terminated });
            
            counterList.Add(new Counter { CounterCategory = "Instance" ,CounterName = "Suspended", CounterValue = Suspended });
            counterList.Add(new Counter { CounterCategory = "Instance" ,CounterName = "Suspended-NR", CounterValue = SuspendedNotResumable });
            return counterList;
        }
    }
}
